package io.renren.modules.business.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.modules.business.entity.EProjectEntity;
import io.renren.modules.business.service.EProjectService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 项目表
 */
@RestController
@RequestMapping("/business/eproject")
public class EProjectController {
    @Autowired
    private EProjectService eProjectService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = eProjectService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{projectId}")
    public R info(@PathVariable("projectId") Long projectId){
		EProjectEntity eProject = eProjectService.getById(projectId);

        return R.ok().put("eProject", eProject);
    }

    /**
     * 项目列表查询
     * @param pageNum
     * @param pageSize
     * @param projectId
     * @return
     */
    @GetMapping("/query")
    public R queryProjectList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(value = "projectId", required = false) Long projectId,
                              @RequestParam(value = "projectName", required = false) String projectName) {
        return R.ok().put("page", eProjectService.queryProjectList(pageNum, pageSize, projectId, projectName));
    }

    /**
     * 保存
     */
    @GetMapping("/save")
    public R save(@RequestParam(value = "projectName") String projectName,
                  @RequestParam(value = "projectLeaderId") Long projectLeaderId,
                  @RequestParam(value = "projectDesc") String projectDesc,
                  @RequestParam(value = "startDate") String startDate,
                  @RequestParam(value = "endDate") String endDate){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        EProjectEntity eProjectEntity = new EProjectEntity();
        eProjectEntity.setProjectName(projectName);
        eProjectEntity.setProjectDesc(projectDesc);
        eProjectEntity.setProjectLeaderId(projectLeaderId);
        try {
            eProjectEntity.setStartDate(simpleDateFormat.parse(startDate));
            eProjectEntity.setEndDate(simpleDateFormat.parse(endDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        eProjectService.save(eProjectEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestParam(value = "projectId") Long projectId,
                    @RequestParam(value = "projectName") String projectName,
                    @RequestParam(value = "projectLeaderId") Long projectLeaderId,
                    @RequestParam(value = "projectDesc") String projectDesc,
                    @RequestParam(value = "startDate") String startDate,
                    @RequestParam(value = "endDate") String endDate){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        EProjectEntity eProjectEntity = new EProjectEntity();
        eProjectEntity.setProjectId(projectId);
        eProjectEntity.setProjectName(projectName);
        eProjectEntity.setProjectDesc(projectDesc);
        eProjectEntity.setProjectLeaderId(projectLeaderId);
        try {
            eProjectEntity.setStartDate(simpleDateFormat.parse(startDate));
            eProjectEntity.setEndDate(simpleDateFormat.parse(endDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
		eProjectService.updateById(eProjectEntity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam("projectId") Long projectId){
		eProjectService.removeById(projectId);

        return R.ok();
    }

}
